#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <limits.h>

#define ITERACOES 1000000000
#define THREADS 8

const unsigned int A = 1664525, C = 1013904223;



unsigned int next(unsigned int x)
{
    return x*A+C;
}

void *MonteCarlo (void *param)
{
    unsigned int rand;
    unsigned long int i, numthr = *((unsigned long int *)param);
    unsigned long int circulo = 0, *ret = (unsigned long int *)param;
    double x, y;

    rand = time(NULL)+numthr;

    for (i = 0; i<ITERACOES/THREADS; i++)
    {
        rand = next(rand);
        x = (double)rand/UINT_MAX;
        rand = next(rand);
        y = (double)rand/UINT_MAX;

        if( ((x*x) + (y*y)) < 1 )
        {
            circulo++;
        }
    }

    (*ret) = circulo;

    pthread_exit(0);
}

int main(void)
{
    int i;
    unsigned long int circulo[THREADS];
    double totalcirculo=0;
    pthread_t ThreadID[THREADS];


    for (i = 0; i < THREADS ; i++)
    {
        circulo[i] = i;
        pthread_create(&ThreadID[i], NULL, MonteCarlo, &(circulo[i]));
    }

    for (i = 0; i < THREADS ; i++)
    {
        pthread_join(ThreadID[i], NULL);
    }

    for (i = 0; i < THREADS ; i++)
    {
        totalcirculo += circulo[i];
    }

    printf("%.6lf\n", (((double)totalcirculo/ITERACOES)*4));

}









